//class ScrollBar
package baidu.ui.controls
{
    import baidu.ui.constants.*;
    import baidu.ui.core.*;
    import baidu.ui.events.*;
    import flash.events.*;

    public class ScrollBar extends baidu.ui.core.BUI
    {
        public function ScrollBar()
        {
            super();
            return;
        }

        public override function setSize(arg1:Number, arg2:Number, arg3:Boolean=true):void
        {
            if (this._orientation != baidu.ui.constants.BUIConstants.HORIZONTAL)
            {
                super.setSize(arg1, arg2, arg3);
            }
            else
            {
                super.setSize(arg2, arg1, arg3);
            }
            return;
        }

        public override function get width():Number
        {
            return this._orientation != baidu.ui.constants.BUIConstants.HORIZONTAL ? super.width : super.height;
        }

        public override function get height():Number
        {
            return this._orientation != baidu.ui.constants.BUIConstants.HORIZONTAL ? super.height : super.width;
        }

        public override function set enabled(arg1:Boolean):void
        {
            super.enabled = arg1;
            var loc1:*;
            this.up.enabled = loc1 = enabled && this._maxScrollPosition > this._minScrollPosition;
            this.thumb.enabled = loc1 = loc1;
            this.track.enabled = loc1 = loc1;
            this.down.enabled = loc1;
            this.updateThumb();
            return;
        }

        public override function get classStyles():Object
        {
            return mergeStyles(baidu.ui.core.BUI.defaultStyles, defaultStyles);
        }

        public function setScrollProperties(arg1:Number, arg2:Number, arg3:Number, arg4:Number=0):void
        {
            this.pageSize = arg1;
            this._minScrollPosition = arg2;
            this._maxScrollPosition = arg3;
            if (arg4 >= 0)
            {
                this._pageScrollSize = arg4;
            }
            this.enabled = this._maxScrollPosition > this._minScrollPosition;
            this.setScrollPosition(this._scrollPosition, false);
            this.updateThumb();
            return;
        }

        public function get scrollPosition():Number
        {
            return this._scrollPosition;
        }

        public function set scrollPosition(arg1:Number):void
        {
            this.setScrollPosition(arg1, true);
            return;
        }

        public function get minScrollPosition():Number
        {
            return this._minScrollPosition;
        }

        public function set minScrollPosition(arg1:Number):void
        {
            this.setScrollProperties(this._pageSize, arg1, this._maxScrollPosition);
            return;
        }

        public function get maxScrollPosition():Number
        {
            return this._maxScrollPosition;
        }

        public function set maxScrollPosition(arg1:Number):void
        {
            this.setScrollProperties(this._pageSize, this._minScrollPosition, arg1);
            return;
        }

        public function get pageSize():Number
        {
            return this._pageSize;
        }

        public function set pageSize(arg1:Number):void
        {
            if (arg1 > 0)
            {
                this._pageSize = arg1;
            }
            return;
        }

        public function get pageScrollSize():Number
        {
            return this._pageScrollSize != 0 ? this._pageScrollSize : this._pageSize;
        }

        public function set pageScrollSize(arg1:Number):void
        {
            if (arg1 >= 0)
            {
                this._pageScrollSize = arg1;
            }
            return;
        }

        public function get lineScrollSize():Number
        {
            return this._lineScrollSize;
        }

        public function set lineScrollSize(arg1:Number):void
        {
            if (arg1 > 0)
            {
                this._lineScrollSize = arg1;
            }
            return;
        }

        public function get orientation():String
        {
            return this._orientation;
        }

        public function set orientation(arg1:String):void
        {
            if (this._orientation == arg1)
            {
                return;
            }
            this._orientation = arg1;
            var loc1:*;
            loc1 = this._orientation == baidu.ui.constants.BUIConstants.HORIZONTAL;
            if (loc1 && rotation == 0)
            {
                rotation = -90;
                scaleX = -1;
            }
            else
            {
                if (!loc1 && rotation == -90)
                {
                    rotation = 0;
                    scaleX = 1;
                }
            }
            invalidate(baidu.ui.core.Invalidation.SIZE);
            return;
        }

        protected override function initUI():void
        {
            super.initUI();
            this.track = new baidu.ui.controls.Button();
            this.track.setPosition(0, WIDTH);
            this.track.useHandCursor = false;
            this.track.autoRepeat = true;
            addChild(this.track);
            this.thumb = new baidu.ui.controls.LabelButton();
            this.thumb.label = "";
            this.thumb.setSize(WIDTH, 15);
            this.thumb.setPosition(0, 15);
            addChild(this.thumb);
            this.down = new baidu.ui.controls.Button();
            this.down.setSize(WIDTH, WIDTH);
            this.down.autoRepeat = true;
            addChild(this.down);
            this.up = new baidu.ui.controls.Button();
            this.up.setSize(WIDTH, WIDTH);
            this.up.setPosition(0, 0);
            this.up.autoRepeat = true;
            addChild(this.up);
            this.up.addEventListener(baidu.ui.events.BUIEvent.BUTTON_REPEAT, this.handleScrollPress, false, 0, true);
            this.down.addEventListener(baidu.ui.events.BUIEvent.BUTTON_REPEAT, this.handleScrollPress, false, 0, true);
            this.track.addEventListener(baidu.ui.events.BUIEvent.BUTTON_REPEAT, this.handleScrollPress, false, 0, true);
            this.thumb.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.handleThumbPress, false, 0, true);
            this.enabled = false;
            return;
        }

        protected override function draw():void
        {
            var loc1:*;
            loc1 = NaN;
            if (isInvalid(baidu.ui.core.Invalidation.SIZE))
            {
                loc1 = _height;
                this.down.setPosition(0, Math.max(this.up.height, loc1 - this.down.height));
                this.track.setSize(WIDTH, Math.max(0, loc1 - (this.down.height + this.up.height)));
                this.updateThumb();
            }
            if (isInvalid(baidu.ui.core.Invalidation.STYLES))
            {
                //loc2 = getSkinInstance(getStyleValue("skin"));
                this.track.setStyle("skin", ResourceFactory.track);
                this.thumb.setStyle("skin", ResourceFactory.trackthumb);
                this.thumb.setStyle("iconSkin", ResourceFactory.thumbIcon);
                this.down.setStyle("skin", ResourceFactory.down);
                this.up.setStyle("skin", ResourceFactory.up);
            }
            this.down.drawNow();
            this.up.drawNow();
            this.track.drawNow();
            this.thumb.drawNow();
            validate();
            return;
        }

        protected function handleScrollPress(arg1:baidu.ui.events.BUIEvent):void
        {
            var loc1:*;
            loc1 = NaN;
            var loc2:*;
            loc2 = NaN;
            arg1.stopImmediatePropagation();
            if (arg1.currentTarget != this.up)
            {
                if (arg1.currentTarget != this.down)
                {
                    loc1 = this.track.mouseY / this.track.height * (this._maxScrollPosition - this._minScrollPosition) + this._minScrollPosition;
                    loc2 = this.pageScrollSize != 0 ? this.pageScrollSize : this.pageSize;
                    if (this._scrollPosition < loc1)
                    {
                        this.setScrollPosition(Math.min(loc1, this._scrollPosition + loc2));
                    }
                    else
                    {
                        if (this._scrollPosition > loc1)
                        {
                            this.setScrollPosition(Math.max(loc1, this._scrollPosition - loc2));
                        }
                    }
                }
                else
                {
                    this.setScrollPosition(this._scrollPosition + this._lineScrollSize);
                }
            }
            else
            {
                this.setScrollPosition(this._scrollPosition - this._lineScrollSize);
            }
            return;
        }

        protected function handleThumbPress(arg1:flash.events.MouseEvent):void
        {
            this.isDragging = true;
            this.thumbScrollOffset = mouseY - this.thumb.y;
            this.thumb.mouseStateLocked = true;
            mouseChildren = false;
            stage.addEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.handleThumbDrag, false, 0, true);
            stage.addEventListener(flash.events.MouseEvent.MOUSE_UP, this.handleThumbRelease, false, 0, true);
            return;
        }

        protected function handleThumbDrag(arg1:flash.events.MouseEvent):void
        {
            var loc1:*;
            loc1 = Math.max(0, Math.min(this.track.height - this.thumb.height, mouseY - this.track.y - this.thumbScrollOffset));
            this.setScrollPosition(loc1 / (this.track.height - this.thumb.height) * (this._maxScrollPosition - this._minScrollPosition) + this._minScrollPosition);
            arg1.updateAfterEvent();
            return;
        }

        protected function handleThumbRelease(arg1:flash.events.MouseEvent):void
        {
            this.isDragging = false;
            mouseChildren = true;
            this.thumb.mouseStateLocked = false;
            if (!stage)
            {
                return;
            }
            stage.removeEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.handleThumbDrag);
            stage.removeEventListener(flash.events.MouseEvent.MOUSE_UP, this.handleThumbRelease);
            return;
        }

        public function setScrollPosition(arg1:Number, arg2:Boolean=true):void
        {
            var loc1:*;
            loc1 = this.scrollPosition;
            this._scrollPosition = Math.max(this._minScrollPosition, Math.min(this._maxScrollPosition, arg1));
            if (loc1 == this._scrollPosition)
            {
                return;
            }
            if (arg2)
            {
                dispatchEvent(new baidu.ui.events.ScrollEvent(this._orientation, this.scrollPosition - loc1, this.scrollPosition));
            }
            this.updateThumb();
            return;
        }

        protected function updateThumb():void
        {
            var loc1:*;
            loc1 = this._maxScrollPosition - this._minScrollPosition + this._pageSize;
            if (this.track.height <= THUMB_MIN_HEIGHT || this._maxScrollPosition <= this._minScrollPosition || loc1 == 0 || isNaN(loc1))
            {
                this.thumb.height = THUMB_MIN_HEIGHT;
                this.thumb.visible = false;
            }
            else
            {
                this.thumb.height = Math.max(THUMB_MIN_HEIGHT + 1, this._pageSize / loc1 * this.track.height);
                this.thumb.y = this.track.y + (this.track.height - this.thumb.height) * (this._scrollPosition - this._minScrollPosition) / (this._maxScrollPosition - this._minScrollPosition);
                this.thumb.visible = enabled;
            }
            return;
        }


        {
            defaultStyles = {"skin":"ScrollBar_Skin"};
            WIDTH = 15;
        }

        public static const THUMB_MIN_HEIGHT:int=33;

        private var _pageSize:Number=10;

        private var _pageScrollSize:Number=0;

        private var _lineScrollSize:Number=1;

        private var _minScrollPosition:Number=0;

        private var _maxScrollPosition:Number=0;

        private var _scrollPosition:Number=0;

        private var _orientation:String="vertical";

        private var thumbScrollOffset:Number;

        protected var isDragging:Boolean=false;

        protected var up:baidu.ui.controls.Button;

        protected var down:baidu.ui.controls.Button;

        protected var thumb:baidu.ui.controls.LabelButton;

        protected var track:baidu.ui.controls.Button;

        public static var defaultStyles:Object;

        public static var WIDTH:int=15;
    }
}


